package com.app.mingluxing.home;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.adapter.ChooseHomeAdapter;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.BindChildList1;
import com.app.mingluxing.bean.RequestSimpleBean;
import com.app.mingluxing.my.AddBabyActivity;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.Constant;
import com.app.mingluxing.utils.DensityUtil;
import com.app.mingluxing.utils.LogUtil;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 选择宝宝页面
 * Created by Aaron on 15/7/30.
 */
public class ChooseHomeBabyActivity extends BaseActivity {

    private ChooseHomeAdapter chooseHomeAdapter;
    private SwipeMenuListView gv_baby;
    private List<BindChildList1.Data> datas;
//    private static final int CHOOSE_BABY_CODE = 10001;
//    private String currentChildId;
//    private String level;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home_choose_baby);
//        currentChildId = sp.getString(Constant.CURRENTCHILDID, "");

        datas = new ArrayList<BindChildList1.Data>();

        initWidget();

        //获取孩子列表
        getChildList();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_title.setText(getResources().getString(R.string.choose_baby));
        tv_right.setText(getResources().getString(R.string.addd));

        gv_baby = (SwipeMenuListView) findViewById(R.id.gv_baby);

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        chooseHomeAdapter = new ChooseHomeAdapter(this, datas);
        gv_baby.setAdapter(chooseHomeAdapter);

        gv_baby.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (datas == null)
                    return;

                if (datas == null || datas.size() == 0)
                    return;

                SharedPreferences.Editor edit = sp.edit();
                edit.putString(Constant.CURRENTCHILDID, datas.get(position).id);
                edit.putString(Constant.CURRENTCHILDNAME, datas.get(position).name);
                edit.putInt(Constant.LEVEL, datas.get(position).level);
                edit.apply();

                EventBus.getDefault().post(1);//提示更换宝宝 刷新页面
                finish();

            }

        });

        //滑动删除
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {

                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());

                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));

                deleteItem.setWidth(DensityUtil.dip2px(ChooseHomeBabyActivity.this, 80));

                deleteItem.setIcon(R.drawable.ic_delete);

                menu.addMenuItem(deleteItem);

            }
        };

        gv_baby.setMenuCreator(creator);

        gv_baby.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // delete
                        if (datas != null && datas.size() > 0)
                            delArea(datas.get(position).id);

                        break;
//                    case 1:
//                        // open
//
//                        LogUtil.e("TAG", "item=====1");
//
//                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });

    }

    private void delArea(String itemId) {

        JSONObject j = new JSONObject();
        j.put("id", itemId);
        j.put("token", sp.getString(Constant.TOKEN, ""));

        HttpUtil.setPostRequest("加载中...", this, Constant.WEB_ROOT + Constant.DELCHILD, j.toJSONString(),
                getClass().getName(), new HttpUtil.SuccessListener() {

            @Override
            public void setOnSuccessListener(String str) {
                try {

                    org.json.JSONObject result = new org.json.JSONObject(str);

                    if (result.getInt("code") == Constant.STATUS_SUCCESS) {

                        showToast("删除成功");
                        getChildList();

                    } else {
                        showToast(Constant.showErrorMsg(result.getInt("code"), ChooseHomeBabyActivity.this));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast(Constant.NODATA);
                }

            }
        }, null);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());
    }

    /**
     * 获取孩子列表
     */
    private void getChildList() {

        RequestSimpleBean requestSimpleBean = new RequestSimpleBean();
        requestSimpleBean.token = sp.getString(Constant.TOKEN, "");
        HttpUtil.setPostRequest("获取孩子列表中...", this, Constant.WEB_ROOT + Constant.GETCHILDERNANDEWATCH, JSONObject.toJSONString(requestSimpleBean),
                getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            BindChildList1 bindChildList1 = JSONObject.parseObject(str, BindChildList1.class);

                            if (bindChildList1.code == Constant.STATUS_SUCCESS) {

                                datas = bindChildList1.data;

                                if (datas == null) {
                                    showToast("请先绑定孩子");
                                    return;
                                }

                                chooseHomeAdapter = new ChooseHomeAdapter(ChooseHomeBabyActivity.this, datas);
                                gv_baby.setAdapter(chooseHomeAdapter);
                                chooseHomeAdapter.notifyDataSetChanged();

                            } else {
                                showToast(Constant.showErrorMsg(bindChildList1.code, ChooseHomeBabyActivity.this));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);

                            LogUtil.e("TAG", "获取学校孩子联系人数据解析错误");

                        }
                    }
                }, null);

    }


    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left://全选或者全不选

                finish();

                break;

            case R.id.tv_right://添加

                Intent intent = new Intent(this, AddBabyActivity.class);
                startActivity(intent);

                break;

        }

    }

}
